package cen.tms.ggtour;



import cen.tms.ggtour.data.DataManager;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Typeface;
import android.location.Location;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CampusMap extends MapActivity {
	//Jonathan Vars
	View infoHolder = null;
	int locID;
	ListView events = null;
	CursorAdapter campusadapter= getAdapter();
	//end Jonathan Vars
	MapView mapView;
	MapController mc;
	GeoPoint p;
	private MyLocationOverlay locationoverlay;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Jonathan Code
        setContentView(R.layout.mapview);
        findViewById(R.id.notifications).setVisibility(View.GONE);
        //end Jonathan Code
        TextView title = (TextView) findViewById(android.R.id.title);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/book_antiqua.ttf");
        title.setTypeface(tf);
        title.setText("Campus Map");
        
        mapView = (MapView)findViewById(R.id.mapview);
        mc = mapView.getController();
        
        locationoverlay = new MyLocationOverlay(this, mapView);
        
        mapView.getOverlays().add(locationoverlay);
        mapView.getOverlays().add(new MapOverlay());
        
        locationoverlay.enableCompass();
        locationoverlay.enableMyLocation();
        
        p = new GeoPoint( (int) (29.6460830 * 1E6),(int) (-82.3477864 * 1E6));
        mc.setZoom(17);
        mc.animateTo(p);
        mapView.invalidate();
       
    }

    @Override
	protected void onResume() {
		super.onResume();
	}
    
    @Override
	protected void onPause() {
		super.onPause();
		locationoverlay.disableCompass();
		locationoverlay.disableMyLocation();
	}
    
    public void onTitleBackClick(View view) {
		this.onBackPressed();
	}
    
	@Override
    protected boolean isRouteDisplayed() { return false; }
	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog beta = null;
		switch(id){
		  case 1:
			beta = new Dialog(this);
			beta.setContentView(R.layout.locationview);
			infoHolder = getLayoutInflater().inflate(R.layout.locationinfo, null);
			events = (ListView)beta.findViewById(R.id.mapEvents);
			events.addHeaderView(infoHolder);
			events.setAdapter(campusadapter);
			events.setOnItemClickListener(new OnItemClickListener(){
	        	//detects clicks on listView, opens display activity
	     		public void onItemClick(AdapterView<?> parent, View subview, int position, long rowid) {
	     			if(position >= 1){
  	     			  //Jonathan Code
	     			  //store _id of Event into bundle
	     			  Bundle thisEvent = new Bundle();
	     			  thisEvent.putInt("_id", DataManager.getID(((Cursor) campusadapter.getItem(position-1))));
	     			  //place bundle into Intent
	     			  Intent displayEvent = new Intent("cen.tms.ggtour.DISPLAY_EVENT");
	     			  displayEvent.putExtras(thisEvent);
	     			  //start display Activity
	     			  startActivity(displayEvent);
	     			  //end Jonathan Code
	     			}
	     		}
	        });
			break;
	 	  default:
			beta = new Dialog(this);
			beta.setContentView(R.layout.beta);
			break;
		}
		return beta;
	}
	protected void onPrepareDialog(int id, Dialog beta){
		switch(id){
		case 1:
			Cursor location = DataManager.getInstance(getApplicationContext()).getLocation(locID);
			TextView info = (TextView)infoHolder.findViewById(R.id.locInfo);
			if(location.moveToFirst()){
		      beta.setTitle(DataManager.getName(location));
			  info.setText(DataManager.getInfo(location));
			  
			  stopManagingCursor(campusadapter.getCursor());
			  campusadapter.changeCursor(DataManager.getInstance(getApplicationContext()).getEvents(null, null, DataManager.getName(location), null ));
			  startManagingCursor(campusadapter.getCursor());
			}
			else{
			  beta.setTitle("");
			  info.setText("");
			}
			location.close();
			break;
		default:
			break;
		}
	}
	class MapOverlay extends com.google.android.maps.Overlay{
	  GeoPoint p1;
	  @Override
	  public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){
	    super.draw(canvas, mapView, shadow);                   
	 	Point screenPts = new Point();
	    if(p1 != null){
	      mapView.getProjection().toPixels(p1, screenPts);	 
	      //---add the marker---
	      Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gators);            
	        canvas.drawBitmap(bmp, screenPts.x, screenPts.y-20, null);
	    }
	    return true;
	  }

      @Override
	  public boolean onTap(GeoPoint p, MapView mapView) {
	    p1 = null;
		Location loc = new Location("Tap Location");
		Location loc2 = new Location("building location");
        loc.setLatitude(p.getLatitudeE6()/1E6);
        loc.setLongitude(p.getLongitudeE6()/1E6);
        Cursor cursor = null;
		cursor = DataManager.getInstance(getApplicationContext()).getLocations(-1, null);
		locID = -1;
		if(cursor.moveToFirst()) {
		  do {
		    double lat = DataManager.getLatitude(cursor);
			double lon = DataManager.getLongitude(cursor);
			loc2.setLatitude(lat);
			loc2.setLongitude(lon);
			if (loc.distanceTo(loc2) <= DataManager.getProximityRadius(cursor)){   
			  locID = DataManager.getID(cursor);
			  showDialog(1);
			  p1 = p;
			break;
			}
		  } while (cursor.moveToNext());
		}
		cursor.close();
		return true;
	  }
	} 
	private CursorAdapter getAdapter(){
		return new CursorAdapter(this, null) {
		
			@Override
			public View newView(Context context, Cursor cursor, ViewGroup parent) {
				LayoutInflater viewInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				View convertView = viewInflater.inflate(R.layout.listevent, parent, false);
				return convertView;
			}
		
			@Override
			public void bindView(View view, Context context, Cursor cursor) {
				TextView txtName = (TextView) view.findViewById(R.id.listEventName);
				TextView txtDate = (TextView) view.findViewById(R.id.listEventDate);
				if (txtName != null) {
					txtName.setText(DataManager.getName(cursor));                       
				}
				if(txtDate != null) {
					txtDate.setText(DataManager.getDate(cursor));     
				}				
			}
		};
	}
}